package kube

import (
	"context"
	"fmt"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/client-go/rest"
	"sigs.k8s.io/cluster-api/controllers/remote"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"testing"
	"time"
)

func TestNewClientFromK8sToken(t *testing.T) {
	token := "eyJhbGciOiJSUzI1NiIsImtpZCI6IllrREc0bjlxYThpQlhmSTVsNmhPdEhvRFR6dTEtZEpFNk82aVdmVTRmd1UifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJtYW5hZ2VtZW50LWFkbWluLXRva2VuLXpsdDh0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6Im1hbmFnZW1lbnQtYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJjMmU2NTY0YS1iM2ZhLTQ0MGEtOGU4ZC1kMmJlOTQwYjI3NDkiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06bWFuYWdlbWVudC1hZG1pbiJ9.ZmedAotoM_7Vqyq9Aa4TR8YGiPqXvNXY_O03aQwk5l3SE4J9vRYxMDY_bjceDL0057YNBqVnRF1X8EQpb5XTRnbmrgwEkEwOPRVUvkTrCLP6vpDoZUctQta6audQBRoKB9cmVRT1quR-xQsqendXXfI8_lqShxF7p4BugOsNlFz_b0x6nvwnAgr8UVcNJfNtR_AyUvtV7Waz4bN1t4Q44LaYsdU4sdDC7RkqN8A2_r93hrN1aGZ4d3IGcjCp4EHvxF1n2J2AXKeRxcbZ_eVhOlRvfpX7PNOD7MypGR1Xb7Lmsrd7sQlKipYN_3540ylLn-W9_cq9VlrFFS-lwwXrIw"
	client, err := NewClientFromK8sToken("192.168.1.232", "36443", token)
	if err != nil {
		return
	}
	nodes, err := client.ListNodes(nil)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Println(len(nodes.Items))

}

func TestNormal(t *testing.T) {
	token := "eyJhbGciOiJSUzI1NiIsImtpZCI6IllrREc0bjlxYThpQlhmSTVsNmhPdEhvRFR6dTEtZEpFNk82aVdmVTRmd1UifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJtYW5hZ2VtZW50LWFkbWluLXRva2VuLXpsdDh0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6Im1hbmFnZW1lbnQtYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJjMmU2NTY0YS1iM2ZhLTQ0MGEtOGU4ZC1kMmJlOTQwYjI3NDkiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06bWFuYWdlbWVudC1hZG1pbiJ9.ZmedAotoM_7Vqyq9Aa4TR8YGiPqXvNXY_O03aQwk5l3SE4J9vRYxMDY_bjceDL0057YNBqVnRF1X8EQpb5XTRnbmrgwEkEwOPRVUvkTrCLP6vpDoZUctQta6audQBRoKB9cmVRT1quR-xQsqendXXfI8_lqShxF7p4BugOsNlFz_b0x6nvwnAgr8UVcNJfNtR_AyUvtV7Waz4bN1t4Q44LaYsdU4sdDC7RkqN8A2_r93hrN1aGZ4d3IGcjCp4EHvxF1n2J2AXKeRxcbZ_eVhOlRvfpX7PNOD7MypGR1Xb7Lmsrd7sQlKipYN_3540ylLn-W9_cq9VlrFFS-lwwXrIw"

	config := &rest.Config{
		BearerToken: token,
		Host:        fmt.Sprintf("https://%s:%s", "192.168.1.238", "36443"),
		TLSClientConfig: rest.TLSClientConfig{
			Insecure: true, // 设置为true时 不需要CA
			//CAData: []byte(c.CA),
		},
	}
	config.UserAgent = remote.DefaultClusterAPIUserAgent("cluster-cache-tracker")
	config.Timeout = 10 * time.Second
	ret, err := client.New(config, client.Options{})
	if err != nil {
		t.Fatal(err)
	}

	nodes := corev1.NodeList{}
	err = ret.List(context.Background(), &nodes)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Println(len(nodes.Items))

}
